﻿using APIShoppingSystem.GetSession;
using APIShoppingSystem.Model;
using IShoppingBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ShoppingEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace APIShoppingSystem.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OrderInfoController : ControllerBase
    {
        private IOrderInfoBll _orderInfoBll;

        public OrderInfoController(IOrderInfoBll orderInfoBll)
        {
            _orderInfoBll = orderInfoBll;
        }


        /// <summary>
        /// 订单表页面分页展示、模糊查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="CommodityName"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult OrderInfoPageList(int page, int limit, string CommodityName)
        {
            int count;
            UserInfo userInfo = this.GetSessionUserInfo();
            object shoppingCarts = _orderInfoBll.GetOrderInfoPageList(page, limit, out count, CommodityName, userInfo);

            return new JsonResult(new
            {
                code = 0,
                msg = "成功",
                count = count,
                data = shoppingCarts
            });
        }

        /// <summary>
        /// 删除订单信息
        /// </summary>
        /// <param name="OrderInfoId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteOrderInfo([FromForm]string OrderInfoId)
        {
            CustomActionResult res = new CustomActionResult();

            if (_orderInfoBll.Delete(OrderInfoId))
            {
                res.Status = 1;
                res.Msg = "成功";
                res.IsSuccess = true;
            }
            return new JsonResult(res);
        }

        /// <summary>
        /// 快递接单方法
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Receipt([FromForm] string orderInfoId)
        {
            CustomActionResult res = new CustomActionResult();

            UserInfo userInfo = this.GetSessionUserInfo();
            bool isSuccess = _orderInfoBll.AddDistributionInfo(orderInfoId, userInfo.ID);
            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Msg = "接单成功";
                res.Status = 1;
                return new JsonResult(res);
            }
            else
            {
                return new JsonResult(res);
            }
        }


        /// <summary>
        /// 确认收货
        /// </summary>
        /// <param name="orderInfoId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Receipts([FromForm] string orderInfoId)
        {
            CustomActionResult res = new CustomActionResult();

            bool isSuccess = _orderInfoBll.Goodsreceipt(orderInfoId);
            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Msg = "收货成功";
                res.Status = 1;
                return new JsonResult(res);
            }
            else
            {
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 发货
        /// </summary>
        /// <param name="orderInfoId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Delivery([FromForm] string orderInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            OrderInfo orderInfo = _orderInfoBll.GetEntity(orderInfoId);
            orderInfo.State = OrderInfo.OrderStateEnum.已发货;
            bool isSuccess = _orderInfoBll.UpdateEntity(orderInfo);
            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Msg = "发货成功";
                res.Status = 1;
                return new JsonResult(res);
            }
            else
            {
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 退货
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Salesreturn([FromForm] string orderInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            string msg;
            bool isSuccess = _orderInfoBll.Salesreturn(orderInfoId, out msg);
            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Status = 1;
                res.Msg = "可以退货";
            }
            else
            {
                res.Msg = msg;
            }
            return new JsonResult(res);
        }

        /// <summary>
        /// 添加退换货记录商品下拉
        /// </summary>
        /// <param name="orderInfoId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult AddSalesreturnViewOption(string orderInfoId)
        {
            CustomActionResult res = new CustomActionResult();

            object OderInsos = _orderInfoBll.AddSalesreturnViewOption(orderInfoId);
            res.Status = 1;
            res.IsSuccess = true;
            res.Msg = "成功";
            res.Datas = OderInsos;
            return new JsonResult(res);
        }


        /// <summary>
        /// 添加退货记录换方法
        /// </summary> 
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddSalesreturn([FromForm] string OrderId,[FromForm] string Reason)
        {
            CustomActionResult res = new CustomActionResult();

            bool isSuccess = _orderInfoBll.AddSalesreturn(OrderId, Reason);
            if (isSuccess)
            {
                res.Status = 1;
                res.IsSuccess = true;
                res.Msg = "成功";
            }

            return new JsonResult(res);
        }

        /// <summary>
        /// 退货
        /// </summary>
        /// <param name="OrderId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Turndestock([FromForm] string orderInfoId)
        {
            CustomActionResult res = new CustomActionResult();

            bool isSuccess = _orderInfoBll.Turndestock(orderInfoId);
            if (isSuccess)
            {
                res.Status = 1;
                res.IsSuccess = true;
                res.Msg = "成功";
            }

            return new JsonResult(res);
        }


        /// <summary>
        ///退款
        /// </summary>
        /// <param name="orderInfoId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Refund([FromForm] string orderInfoId)
        {
            CustomActionResult res = new CustomActionResult();

            bool isSuccess = _orderInfoBll.Refund(orderInfoId);
            if (isSuccess)
            {
                res.Status = 1;
                res.IsSuccess = true;
                res.Msg = "成功";
            }

            return new JsonResult(res);
        }
    }
}
